package cs575.wido.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import cs575.wido.action.EditUserAccountAction;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserAccountWindow extends JDialog {
	private JTextField txtUsername;
	private JTextField txtEmail;
	private JPasswordField pwdPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UserAccountWindow dialog = new UserAccountWindow();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UserAccountWindow() {
		setTitle("User Account");
		setBounds(100, 100, 450, 300);
		
		getContentPane().add(buildFormPanel(), BorderLayout.CENTER);
		getContentPane().add(buildButtonPane(), BorderLayout.SOUTH);
	}
	
	private JComponent buildFormPanel() {
		JPanel contentPanel = new JPanel();
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("right:default"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblId = new JLabel("ID");
		contentPanel.add(lblId, "2, 2");
		
		JLabel lblUserName = new JLabel("User Name");
		contentPanel.add(lblUserName, "2, 4, right, default");
		
		txtUsername = new JTextField();
		contentPanel.add(txtUsername, "4, 4, fill, default");
		txtUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		contentPanel.add(lblPassword, "2, 6, right, default");
		
		pwdPassword = new JPasswordField();
		pwdPassword.setEditable(false);
		contentPanel.add(pwdPassword, "4, 6, fill, default");
		
		JLabel lblEmail = new JLabel("Email");
		contentPanel.add(lblEmail, "2, 8, right, default");
		
		txtEmail = new JTextField();
		contentPanel.add(txtEmail, "4, 8, fill, default");
		txtEmail.setColumns(10);
		
		return contentPanel;
	}
	
	private JComponent buildButtonPane() {
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.setAction(new EditUserAccountAction(this));
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserAccountWindow.this.dispose();
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
		
		JButton btnResetPassword = new JButton("Reset Password");
		btnResetPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserAccountWindow.this.pwdPassword.setEditable(true);
			}
		});
		buttonPane.add(btnResetPassword);
		
		return buttonPane;
	}
}
